package com.block.sms.utils;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class JiGuangUtils {

        public static void main(String[] args) throws Exception {

            /**
             * GXQJvnAoO/49OaV86PkJD6isWZH2JGCAJYBEZ2COR7Q2b2Hgh6DL/40aPRiihgZcV6+jjUUPZK98UcXwKAOcvVA9Dl3P1IMZ8H0XxwFdH4lFlhEA4Cef6D4/Y4QdiJPmrePUo8uk2gJ0UgWU8ZS8MY15Gn4ATggEZZ+JudYt/I4=
             */

            String encrypted = "GXQJvnAoO/49OaV86PkJD6isWZH2JGCAJYBEZ2COR7Q2b2Hgh6DL/40aPRiihgZcV6+jjUUPZK98UcXwKAOcvVA9Dl3P1IMZ8H0XxwFdH4lFlhEA4Cef6D4/Y4QdiJPmrePUo8uk2gJ0UgWU8ZS8MY15Gn4ATggEZZ+JudYt/I4=";
            String prikey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJJAIDovgv6ynVxJgxc1a/YGLqY61RpTmWexWkvLr9kmRZTlz/4G2qOiOnr6MxqjOC5D/F5RHQza1iezme+8vy492zVVJLwjT76KJ1TRDq/PLQCgcR9eNDObJlW1tlEBAeeq8TwzQ7orq0V4/lS+mwcAIVg2xC0oaStc1KLv6plDAgMBAAECgYASXRTu2WpkWr8hiGV6c43DzZDr9HUvohGgEIfa/o9s/7467QRJzEiFxWf8axAhpx71yDmXCi5Qn+XGBGouzQ9gKVteHQnNST05k9r9FYWV396/g8M7xt0jvtF4ReZtbEdHjKMXRXj/FnvFcMgeBs9GUBQvaYODsWzi9+IuFQlJaQJBAM2v1FT6/zjN5Wb82IlhyCHQn6J7e7/hFqyBcVyoYKtFVpzN29PtI748VyroZYoT++1QfA0DETXIVIOOT9v0h50CQQC2BmAGC50m5+yUC3CpBvxZ0AA5woJr7gzpgRz8mzpieDl0GF3Vw4P+59sX4Cbsr7MVuYM6/cQoncc9KaSEEX5fAkBf+/hzO7oUUQkHeWC3I5lYJloIYeWK0khZcgJrwetyoWXcwLgf+p7ebJwk2tRfjhnXUeLB07eZSIYf6i5cL42FAkAlfWalmBrD+pyVWU3Y4mFzNg3114P5KsxqA3eu52G9Cjn7GVGqR1yWUbakUjxyHzXpUY4fxkhyfRyeEkeaE+hNAkA+USl3z4WYBcv5XvNlBxp5eCx1cEIOHiY3aE694ytVz0+9oQh1zRedEkwyDft7ZlK0y4quGZy7oAp9VHUxu7BN";

            String result = decrypt(encrypted, prikey);
            System.out.println(result);
        }

        public static String decrypt(String cryptograph, String prikey) throws Exception {

            byte[] decode = Base64.getDecoder().decode(prikey);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decode);
            PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);

            Cipher cipher=Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);

            byte[] bytes = Base64.getDecoder().decode(cryptograph);
            return new String(cipher.doFinal(bytes));
        }


}
